package com.project.user.service.impl;

import java.util.List;
import java.util.Map;

import com.project.common.utils.TimeUuidUtil;
import com.project.system.domain.GoodsInfo;
import com.project.system.mapper.GoodsInfoMapper;
import com.project.user.domain.UserOrderDetail;
import com.project.user.mapper.UserCartMapper;
import com.project.user.mapper.UserOrderDetailMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.project.user.mapper.UserOrderMapper;
import com.project.user.domain.UserOrder;
import com.project.user.service.IUserOrderService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 订单列 服务层实现
 *
 * @author administrator
 */
@Service
public class UserOrderServiceImpl implements IUserOrderService {
    @Autowired
    private UserOrderMapper userOrderMapper;
    @Autowired
    private UserOrderDetailMapper orderDetailMapper;
    @Autowired
    private UserCartMapper userCartMapper;
    @Autowired
    private GoodsInfoMapper goodsInfoMapper;

    /**
     * 查询订单列信息
     *
     * @param orderId 订单列ID
     * @return 订单列信息
     */
    @Override
    public UserOrder selectUserOrderById(Long orderId) {
        return userOrderMapper.selectUserOrderById(orderId);
    }

    /**
     * 查询订单列列表
     *
     * @param userOrder 订单列信息
     * @return 订单列集合
     */
    @Override
    public List<UserOrder> selectUserOrderList(UserOrder userOrder) {
        return userOrderMapper.selectUserOrderList(userOrder);
    }

    /**
     * 后台管理员查询订单
     *
     * @param userOrder
     * @return
     */
    @Override
    public List<UserOrder> selectSystemOrderList(UserOrder userOrder) {
        return userOrderMapper.selectSystemOrderList(userOrder);
    }

    /**
     * 新增订单列
     *
     * @param userOrder 订单列信息
     * @return 结果
     */
    @Override
    @Transactional
    public String insertUserOrder(UserOrder userOrder, Integer isFromCart, String isvip) {
        GoodsInfo info = new GoodsInfo();
        List<UserOrderDetail> details = userOrder.getDetails();
        String orderIds = "";
        for (UserOrderDetail detail : details) {
            Long orderId = TimeUuidUtil.get16UUID();
            detail.setOrderId(orderId);
            orderDetailMapper.insertUserOrderDetail(detail);
            if (isFromCart == 1) {
                detail.getParams().put("userId", userOrder.getUserId());
                userCartMapper.deleteUserCartByOrder(detail);
            }
            //更新数量
            info.setGoodsId(detail.getGoodsId());
            info.setGoodsLeft(detail.getGoodsNum() * (-1d));
            goodsInfoMapper.updateGoodsLeft(info);
            if ("Y".equals(isvip)) {
                userOrder.setTotalPrice(detail.getGoodsPrice() * detail.getGoodsNum() * 0.9);
            } else {
                userOrder.setTotalPrice(detail.getGoodsPrice() * detail.getGoodsNum());
            }
            userOrder.setOrderId(orderId);
            userOrderMapper.insertUserOrder(userOrder);
            orderIds += "," + orderId;
        }
        return orderIds;
    }

    /**
     * 修改订单列
     *
     * @param userOrder 订单列信息
     * @return 结果
     */
    @Override
    public int updateUserOrder(UserOrder userOrder) {
        return userOrderMapper.updateUserOrder(userOrder);
    }

    /**
     * 删除订单列对象
     *
     * @param id 需要删除的数据ID
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteUserOrderById(Long id) {
        orderDetailMapper.deleteUserOrderDetailByOrderId(id);
        return userOrderMapper.deleteUserOrderById(id);
    }

    @Override
    public int updateUserOrderStatus(UserOrder userOrder) {
        if (userOrder.getStatus() == 5) {
            //取消订单时，餐饮剩余回归
            UserOrderDetail orderDetail = new UserOrderDetail();
            orderDetail.setOrderId(userOrder.getOrderId());
            List<UserOrderDetail> list = orderDetailMapper.selectUserOrderDetailList(orderDetail);
            GoodsInfo info = new GoodsInfo();
            for (UserOrderDetail detail : list) {
                info.setGoodsId(detail.getGoodsId());
                info.setGoodsLeft(detail.getGoodsNum() * 1d);
                goodsInfoMapper.updateGoodsLeft(info);
            }
        }
        return userOrderMapper.updateUserOrderStatus(userOrder);
    }

    /**
     * 统计各个餐饮种类销量
     *
     * @param userOrder
     * @return
     */
    public List<Map<String, Object>> countGoodsNumByType(UserOrder userOrder) {
        return userOrderMapper.countGoodsNumByType(userOrder);
    }

    /**
     * 统计各个阶段销量
     *
     * @param userOrder
     * @return
     */
    public List<Map<String, Object>> countMoneyByQuetar(UserOrder userOrder) {
        return userOrderMapper.countMoneyByQuetar(userOrder);
    }

    /**
     * 统计下单次数
     *
     * @return
     */
    public List<Map<String, Object>> countByGoodsId() {
        return userOrderMapper.countByGoodsId();
    }
}
